#!/bin/sh

main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html


ignore=`uci get dhcp.lan.ignore 2>/dev/null || echo 0`

cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-2-5">
				<h3>Lan设置</h3>
EOF
lans=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_available_lanzone no="ext_lan"`
for lanzone in $lans
do
cat <<EOF
<script>
\$(function(){
  \$('#${lanzone}_setting').on('submit', function(e){
    e.preventDefault();
    var data = "app=lan&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'ajax-fluid');
  });
});
</script>
EOF
cat <<EOF
			  <h3>${lanzone}</h3>
			  <form id="${lanzone}_setting">
				<table class="pure-table pure-table-bordered">

				<tr>
				<td>
				启用DHCP
				</td>
				<td>
				<input type="checkbox" name="ignore" value="0" `[ $ignore -eq 0 ] && echo checked`>
				</td>
				</tr>

				<tr>
				<td>
				网关地址
				</td>
				<td>
				<input type="text" name="ipaddr" value="`uci get network.${lanzone}.ipaddr 2>/dev/null`">
				</td>
				</tr>

				<tr>
				<td>
				网关掩码
				</td>
				<td>
				<input type="text" name="netmask" value="`uci get network.${lanzone}.netmask 2>/dev/null`">
				</td>
				</tr>

				<tr>
				<td>
				内网起始IP
				</td>
				<td>
				<input type="text" name="start" value="`uci get dhcp.${lanzone}.start 2>/dev/null`">
				</td>
				</tr>

				<tr>
				<td>
				内网单网段IP数限制
				</td>
				<td>
				<input type="text" name="limit" value="`uci get dhcp.${lanzone}.limit 2>/dev/null`">
				</td>
				</tr>

				<tr>
				<td>
				DHCP过期时间
				</td>
				<td>
				<input type="text" name="leasetime" value="`uci get dhcp.${lanzone}.leasetime 2>/dev/null`">
				</td>
				</tr>

				<tr>
				<td>
				操作
				</td>
				<td>
				<input type="hidden" name="action" value="post_save_lan">
				<input type="hidden" name="lanzone" value="${lanzone}">
				<button type="submit" class="pure-button">保存</button>
				</td>
				</tr>
				</table>
			  </form>
EOF

done
cat <<EOF
			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-3-5">
				<h3>DHCP 客户列表</h3>
<pre style="font-size:small">
`cat /tmp/dhcp.leases`
</pre>
			</div>
		</div>
EOF
if
[ $(echo "$lans" | wc -l) -eq 1 ]
then
footer_html keep_bottom
else
footer_html
fi
}
post_save_lan()
{
if
[ -n "$FORM_ipaddr" ] && \
[ -n "$FORM_netmask" ] && \
[ -n "$FORM_start" ] && \
[ -n "$FORM_limit" ] && \
[ -n "$FORM_leasetime" ]
then
main.sbin set_lan
echo "局域网设置成功!!" | main.sbin output_json 0
else
echo "局域网设置失败!!" | main.sbin output_json 1
fi
}

lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi

